package com.jx.jingxuan.widget.autoViewPager;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.jx.jingxuan.util.Tools;

/**
 * <li>Package: {@link IndicatorView}
 * <li>Author: cyh
 * <li>Date: 2017/6/22 10:05
 * <li>Discription: 轮播图底部的矩形框
 */

public class IndicatorView extends View {
    public static final int C_RADIUS = 4;
    public static final int C_SPACE = 8;
    public static final int C_NORMAL_COLOR = Color.GRAY;
    public static final int C_SELECT_COLOR = Color.WHITE;

    private float cRadius;//矩形框圆角半径
    private float cSpace;//矩形框间距
    private float rountRectWidth;//矩形框长度
    private int cCount;//矩形框数量
    private int cNormalColor;//矩形框普通状态下的颜色
    private int cSelectColor;//矩形框选中状态下的颜色

    private RectF currentRectF = new RectF();//当前选中矩形框

    //画笔
    private Paint normalPaint;
    private Paint selectPaint;

    public IndicatorView(Context context) {
        super(context);
        init();
    }

    public IndicatorView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public IndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        //参数初始化
        cRadius = Tools.dip2px(getContext().getApplicationContext(), C_RADIUS);
        cSpace = Tools.dip2px(getContext().getApplicationContext(), C_SPACE);
        rountRectWidth = cRadius * 2;
        cNormalColor = C_NORMAL_COLOR;
        cSelectColor = C_SELECT_COLOR;

        normalPaint = new Paint();
        normalPaint.setAntiAlias(true);
        normalPaint.setColor(cNormalColor);

        selectPaint = new Paint();
        selectPaint.setAntiAlias(true);
        selectPaint.setColor(cSelectColor);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (cCount > 0) {
            computeSize();
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setcRadius(float cRadius) {
        this.cRadius = cRadius;
    }

    public void setRountRectWidth(float rountRectWidth) {
        this.rountRectWidth = rountRectWidth;
    }

    public void setcSpace(float cSpace) {
        this.cSpace = cSpace;
    }

    /**
     * 开始绘制矩形框
     *
     * @param cCount
     */
    public void startDraw(int cCount) {
        this.cCount = cCount;
        computeSize();
        invalidate();
    }

    private void computeSize() {
        RectF rectF = completeRountRectPosition(cCount - 1);
        //通过矩形框的个数计算出控件宽高
        int width = (int) Math.ceil(rectF.right);
        int height = (int) Math.ceil(rectF.bottom);
        setMeasuredDimension(width, height);
    }

    /**
     * 普通矩形框的颜色
     *
     * @param cNormalColor
     */
    public void setcNormalColor(int cNormalColor) {
        this.cNormalColor = cNormalColor;
        normalPaint.setColor(cNormalColor);
    }

    /**
     * 选中矩形框的颜色
     *
     * @param cSelectColor
     */
    public void setcSelectColor(int cSelectColor) {
        this.cSelectColor = cSelectColor;
        selectPaint.setColor(cSelectColor);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画普通矩形框
        for (int i = 0; i < cCount; i++) {
            RectF rectF = completeRountRectPosition(i);
            canvas.drawRoundRect(rectF, cRadius, cRadius, normalPaint);
        }

        //画选中矩形框
        if (currentRectF.left < 0) {//不能滑出左边界
            currentRectF.left = 0;
            currentRectF.right = rountRectWidth;
        } else if (currentRectF.right > getWidth()) {//不能滑出右边界
            currentRectF.right = getWidth();
            currentRectF.left = currentRectF.right - rountRectWidth;
        }
        canvas.drawRoundRect(currentRectF, cRadius, cRadius, selectPaint);
    }

    /**
     * 计算出每个矩形框的坐标
     *
     * @param position
     * @return
     */
    private RectF completeRountRectPosition(int position) {
        RectF rectF = new RectF();
        rectF.left = position * (rountRectWidth + cSpace);
        rectF.top = 0;
        rectF.right = rectF.left + rountRectWidth;
        rectF.bottom = cRadius * 2;
        return rectF;
    }

    /**
     * 移动被选中的矩形框
     *
     * @param position       第几张图
     * @param positionOffset 偏移量
     */
    public void moveSelectRountRect(int position, float positionOffset) {
        currentRectF = completeRountRectPosition(position);
        float rSpace = rountRectWidth + cSpace;
        currentRectF.left = currentRectF.left + rSpace * positionOffset;
        currentRectF.right = currentRectF.left + rountRectWidth;
        invalidate();
    }
}
